package data;

public class Mandelbrot
{

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		for(int i=1;i<50;i++){
			double side = 2./(i*i);
			System.out.println("side : " + side);
			calculate(side,i);
			
		}
		
	}

	private static void calculate(double side,  int index) {
		double x=-0.10902;
		double y=0.901;
		
		double x1 = x-side/2;
		double x2 = x+side/2;
		double y1 = y-side/2;
		double y2 = y+side/2;

		int lenght = 2000;
		int width = 2000;
		double xstep = (x2 - x1) / lenght;
		double ystep = (y2 - y1) / width;

		int totalIteractions = 1000;
		//double  gamma=16777215/totalIteractions;
		double gamma = 255./totalIteractions;

		int[][] rgbValues = new int[lenght][width];

		System.out.println("start");
		for (int i = 0; i < width; i++)
		{
			for (int j = 0; j < lenght; j++)
			{

				double r = x1 + (double) i * xstep;
				double c = y1 + (double) j * ystep;
				//System.out.println(real + "," + complex);
				int iter = 0;
				boolean div = false;
				double mod = 0;
				double real=r;
				double complex=c;
				while (iter <= totalIteractions && !div)
				{

					double realN = real * real - complex * complex + r;
					double complexN = 2 * real * complex + c;
					mod = realN * realN + complexN * complexN;
					if (mod > 2)
						div = true;
					real = realN;
					complex=complexN;
					iter++;
				}

				if (iter >= totalIteractions)
				{
					rgbValues[j][i] = 0;
				}
				else
				{
					int red= 0;
					int green=(int) (gamma*iter);
					//System.out.println(iter+"\t"+green);
					int blue = 0;
					rgbValues[i][j]=red|green<<8|blue<<16;
					//rgbValues[j][i] =16777215;
				}
			}
		}

		rgbValues[lenght/2][width/2] = 255 | 255 << 8 | 0 << 16;
		
		System.out.println("Stop");

		BMP bmp = new BMP();
		long time = System.currentTimeMillis();
		bmp.saveBMP("H:/bmp/mb_"+index+"_"+side+".bmp", rgbValues);
		
	}
}
